package cn.juque.lucenecandy.threads;

import cn.hutool.core.thread.ThreadUtil;
import cn.juque.lucenecandy.cache.IpCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.*;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>HeartbeatThreadPool</li>
 * </ul>
 * @date 2024-01-05
 **/
@Slf4j
@Component("heartbeatThreadPool")
public class HeartbeatThreadPool {

    private static final ThreadFactory THREAD_FACTORY = ThreadUtil.newNamedThreadFactory("heart-beat-", true);

    private static final ScheduledExecutorService EXECUTOR =
            new ScheduledThreadPoolExecutor(1, THREAD_FACTORY, new ThreadPoolExecutor.DiscardPolicy());

    @Resource
    private IpCache ipCache;

    public void init() {
        EXECUTOR.scheduleWithFixedDelay(() -> this.ipCache.refresh(), 0, 5, TimeUnit.SECONDS);
        log.debug("success init heart beat ThreadPool");
    }
}
